﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
/// <summary>
/// DBA 的摘要说明
/// </summary>
public class DBA
{
    public static string getstrConnection(string db, string server)
    {
        try
        {
            if (db == null)
            {
                db = "rxjhgame";
            }
            if (db == "PublicDb")
            {
                return ConfigurationManager.AppSettings["PublicDb"].ToString();
            }
            string sql = string.Format("select * from serverlist WHERE serverid = @serverid");
            SqlParameter[] prams22 = {
                SqlDBA.MakeInParam("@serverid", SqlDbType.VarChar, 30, server)
            };
            DataTable table2 = DBAi.GetDBToDataTable(sql, prams22);

            if (table2 != null)
            {
                if (table2.Rows.Count > 0)
                {
                    if (db == "rxjhaccount")
                    {
                        return string.Format("Data Source={0};uid={1};pwd={2};database={3};Packet Size=4096;Pooling=true;Max Pool Size=512;Min Pool Size=1", table2.Rows[0]["DbIp"].ToString(), table2.Rows[0]["DbUser"].ToString(), table2.Rows[0]["DbPass"].ToString(), table2.Rows[0]["DbAccount"].ToString());
                    }
                    else
                    {
                        return string.Format("Data Source={0};uid={1};pwd={2};database={3};Packet Size=4096;Pooling=true;Max Pool Size=512;Min Pool Size=1", table2.Rows[0]["DbIp"].ToString(), table2.Rows[0]["DbUser"].ToString(), table2.Rows[0]["DbPass"].ToString(), table2.Rows[0]["DbGame"].ToString());
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
            //DbClassLst ddlist;
            //if (Global.Xwwl_Db.TryGetValue(server, out ddlist))
            //{
            //    DbClass dd;
            //    if (ddlist.Db.TryGetValue(db, out dd))
            //    {
            //        return dd.SqlConnect;
            //    }
            //    else
            //    {
            //        return null;
            //    }
            //}
            //else
            //{
            //    return null;
            //}
        }
        catch
        {
            return null;
        }

    }

    public static int ExeSqlCommand(string sqlCommand, string db, string server)
    {
        using (SqlConnection conn = new SqlConnection(DBA.getstrConnection(db, server)))
        {
            using (SqlCommand command1 = new SqlCommand(sqlCommand, conn))
            {
                int num1 = -1;
                try
                {
                    conn.Open();
                }
                catch
                {
                    return -1;
                }
                try
                {
                    num1 = command1.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    // Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand);
                    //WebFun.ScriptMessage("SQL错误:" + exception1.Message, true);
                }
                finally
                {
                    command1.Dispose();
                    conn.Close();
                    conn.Dispose();
                }
                return num1;
            }
        }
    }

    public static DataTable GetDBToDataTable(string sqlCommand, string db, string server)
    {
        using (SqlConnection conn = new SqlConnection(DBA.getstrConnection(db, server)))
        {
            using (SqlDataAdapter adapter1 = new SqlDataAdapter())
            {
                using (adapter1.SelectCommand = new SqlCommand(sqlCommand, conn))
                {
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        //Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand);
                        return null;
                    }
                    DataTable table1 = new DataTable();
                    try
                    {
                        adapter1.Fill(table1);
                    }
                    catch (Exception ex)
                    {
                        //Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand);
                    }
                    adapter1.Dispose();
                    conn.Close();
                    conn.Dispose();
                    return table1;
                }
            }
        }
    }

    public static object GetDBValue_3(string sqlCommand, string db, string server)
    {
        using (SqlConnection connection1 = new SqlConnection(DBA.getstrConnection(db, server)))
        {
            using (SqlCommand command1 = new SqlCommand(sqlCommand, connection1))
            {
                try
                {
                    connection1.Open();
                }
                catch
                {
                    return null;
                }
                object obj1 = null;
                try
                {
                    obj1 = command1.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    //Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand);
                    //logo.ErrLog("DBA数据层_错误", ex, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                    //log.ErrLog(ex);
                }
                command1.Dispose();
                connection1.Close();
                connection1.Dispose();
                return obj1;
            }
        }
    }
    public static DataSet GetDataSet(string sql, string db, string server)
    {
        //string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnString"];

        SqlConnection connection1 = new SqlConnection(DBA.getstrConnection(db, server));

        SqlDataAdapter sda = new SqlDataAdapter(sql, connection1);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        return ds;
    }
    public static DataSet GetList(string TBName, int PageSize, int CurPage, string KeyField, string Condition, string Order, string db, string server)
    {
        DataSet dataReader = null;
        SqlConnection conn = new SqlConnection(DBA.getstrConnection(db, server));
        //			SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
        try
        {
            SqlParameter[] prams = {
                                           SqlDBA.MakeInParam("@TBName", SqlDbType.NVarChar, 100, TBName),
                                           SqlDBA.MakeInParam("@PageSize", SqlDbType.Int,10, PageSize),
                                           SqlDBA.MakeInParam("@CurPage", SqlDbType.Int, 10, CurPage),
                                           SqlDBA.MakeInParam("@KeyField", SqlDbType.NVarChar, 100, KeyField),
                                           SqlDBA.MakeInParam("@Condition", SqlDbType.NVarChar, 200, Condition),
                                           SqlDBA.MakeInParam("@Order", SqlDbType.NVarChar, 200, Order)
                                       };
            SqlDBA.RunProc(conn, "XW_PublicTurnPageWebSite", prams, out dataReader);
        }
        catch (Exception exception1)
        {
            //WebFun.ScriptMessage("SqlDBA发生错误，信息为：" + exception1.Message, true);
        }
        return dataReader;
    }
    public static DataSet GetList(string TBName, int PageSize, int CurPage, string KeyField, string Condition, string Order, string KeyAscDesc, string db, string server)
    {
        DataSet dataReader = null;
        SqlConnection conn = new SqlConnection(DBA.getstrConnection(db, server));
        //			SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
        try
        {
            SqlParameter[] prams = {
                                           SqlDBA.MakeInParam("@TBName", SqlDbType.NVarChar, 100, TBName),
                                           SqlDBA.MakeInParam("@PageSize", SqlDbType.Int,10, PageSize),
                                           SqlDBA.MakeInParam("@CurPage", SqlDbType.Int, 10, CurPage),
                                           SqlDBA.MakeInParam("@KeyField", SqlDbType.NVarChar, 100, KeyField),
                                           SqlDBA.MakeInParam("@KeyAscDesc", SqlDbType.NVarChar, 4, KeyAscDesc),
                                           SqlDBA.MakeInParam("@Condition", SqlDbType.NVarChar, 200, Condition),
                                           SqlDBA.MakeInParam("@Order", SqlDbType.NVarChar, 200, Order)
                                       };
            SqlDBA.RunProc(conn, "XW_PublicTurnPageWebSite", prams, out dataReader);
        }
        catch (Exception exception1)
        {
            //WebFun.ScriptMessage("SqlDBA发生错误，信息为：" + exception1.Message, true);
        }
        return dataReader;
    }

    public static DataSet GetNewList(string 表名, string 显示字段, int 每页显示数, int 当前页, string 查询条件, string 表的主键, string 排序字段, int 排序方法, out int 总页数, out int 记录数, string db, string server)
    {
        DataSet dataReader = null;
        SqlConnection conn = new SqlConnection(DBA.getstrConnection(db, server));
        //			SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
        总页数 = 0;
        记录数 = 0;
        try
        {

            SqlParameter[] prams = {
                                           SqlDBA.MakeInParam("@tblName", SqlDbType.NVarChar, 200, 表名),
                                           SqlDBA.MakeInParam("@fldName", SqlDbType.NVarChar, 500, 显示字段),
                                           SqlDBA.MakeInParam("@pageSize", SqlDbType.Int,10, 每页显示数),
                                           SqlDBA.MakeInParam("@page", SqlDbType.Int, 10, 当前页),
                                           SqlDBA.MakeInParam("@strCondition", SqlDbType.NVarChar, 1000, 查询条件),
                                           SqlDBA.MakeInParam("@ID", SqlDbType.NVarChar, 150, 表的主键),
                                           SqlDBA.MakeInParam("@fldSort", SqlDbType.NVarChar, 200, 排序字段),
                                           SqlDBA.MakeInParam("@Sort", SqlDbType.Bit, 10, 排序方法),
                                           SqlDBA.MakeOutParam("@pageCount", SqlDbType.Int,4),
                                           SqlDBA.MakeOutParam("@Counts", SqlDbType.Int,4)
                                       };
            SqlDBA.RunProc(conn, "proc_ListPageInt", prams, out dataReader);
            总页数 = Convert.ToInt32(prams[8].Value);
            记录数 = Convert.ToInt32(prams[9].Value);
        }
        catch (Exception exception1)
        {
            //WebFun.ScriptMessage("SqlDBA发生错误，信息为：" + exception1.Message, true);
            throw new ArgumentException("SqlDBA发生错误，信息为：" + exception1.Message);
        }
        return dataReader;
    }
}